
********************************* Merging Remaining Data ************************************
clear all

global folder "~\Dropbox\cartelle condivise\Ash_Morelli_Vannoni\_papers\2-complexity-growth\replication_23_5_24"

cd "$folder/data"   

use bartik_shocks_final, clear
rename stateabrev state_abbrev
tsset state year, delta(2)

merge 1:1 state_abbrev year using previous_results
drop if _merge==2
drop _merge

merge 1:1 statename year using frase_index_2017
drop if _merge==2
drop _merge

merge m:1 year using total_quantgov
drop if _merge==2
drop _merge

gen fed_reg=frase_current*totalrestrictions

merge 1:1 state_abbrev year using dataset_pagecount
drop if _merge==2
drop _merge

merge 1:1 state_abbrev year using dataset_unc
drop if _merge == 2
drop _merge

merge 1:1 state_abbrev year using "$folder/data/input/state_year_profits"
drop if _merge==2
drop _merge


drop if state==.
merg 1:1 statename year using gov_controls
drop if _merge==2
drop _merge


merge 1:1 state_abbrev year using "$folder/data/input/cpb-1962-1973"
drop if _merge==2
drop _merge

merge 1:1 state_abbrev year using "$folder/data/input/cpb-1974-1985", update
drop if _merge==2
drop _merge

merge 1:1 state_abbrev year using "$folder/data/input/cpb-1986-1998", update
drop if _merge==2
drop _merge

merge m:1 state_abbrev using "$folder/data/input/topic-clusters", update
drop if _merge==2
drop _merge

merge 1:1 state_abbrev year  using dataset_state_news_uncertain, generate(gh)
drop if gh == 2
drop gh


gen estab_small = .
replace estab_small = estab_n4_7 + estab_n8_19 if !missing(estab_n4_7)
replace estab_small = estab_n5_9 + estab_n10_19 if !missing(estab_n5_9)

gen estab_mid = estab_n20_49 + estab_n50_99 + estab_n100_249

gen estab_large = .
replace estab_large = estab_n250_499 + estab_n500 if !missing(estab_n500)
replace estab_large = estab_n250_499 + estab_n500_999 + estab_n1000 if !missing(estab_n1000)

sort state year
foreach x in estab estab_small estab_mid estab_large profits_mill cbp_ap cbp_emp {
gen l`x' = log(`x')
gen l`x'_D = D.l`x'

}

drop _merge

merge 1:1 state_abbrev year using "$folder/data/input/dataset_cond1"
drop if _merge == 2
drop _merge

merge 1:1 state_abbrev year using sector-spec, gen(gg)
drop if gg == 2
drop gg

merge 1:1 state_abbrev year using data_statereg
drop _merge

merge 1:1 state_abbrev year using data_court
drop _merge

merge 1:1 state_abbrev year using campaign
drop _merge

merge 1:1 state_abbrev year using lobbying
drop _merge

save data_ready.dta, replace

drop if state==.
*********************************** Final Preparations ***********************************

xtset state year, yearly delta(2)

foreach x in 6 12 18 24 30 36 42 48 {
	foreach y in shock_prov`x' shock_word`x' shock_cont_prov`x' shock_non_cont_prov`x' shock_cond_list_prov`x' shock_non_cond_list_prov`x' shock_cond_prov`x' shock_non_cond_prov`x' shock_constr`x' shock_permis`x' log_provisions_`x' {
	
		gen F_`y' = F.`y'
		
		gen F2_`y' = F2.`y'

		gen L_`y' = L.`y'

		gen L2_`y'= L2.`y'
		
		gen L3_`y'= L3.`y'

	}
 }
 
 foreach y in econ soc fisc proc {
 
		gen F_log_provisions_42`y' = F.log_provisions_42`y'
		
		gen F2_log_provisions_42`y' = F2.log_provisions_42`y'

		gen L_log_provisions_42`y' = L.log_provisions_42`y'

		gen L2_log_provisions_42`y'= L2.log_provisions_42`y'
		
		gen L3_log_provisions_42`y'= L3.log_provisions_42`y'
		
		gen F_shock_prov42`y' = F.shock_prov42`y'
		
		gen F2_shock_prov42`y' = F2.shock_prov42`y'

		gen L_shock_prov42`y' = L.shock_prov42`y'

		gen L2_shock_prov42`y'= L2.shock_prov42`y'
		
		gen L3_shock_prov42`y'= L3.shock_prov42`y'
 
 
 }
 


***** Income per capita
gen loginc = log(real1incpc)
gen loginc2 = loginc^2
gen loginc_D=D.loginc

gen logrev = log(rev)
gen logrev2 = logrev^2 
gen logexp = log(exp)
gen logexp2 = logexp^2
gen loglegexp = log(leg)
gen loglegexp2 = loglegexp^2

gen provratio = provisions_18 / word_count_18
gen logprovratio = log(provratio)
gen logprovratio_D = D.logprovratio

gen provratio_p = provisions_18 /page_count
gen logprovratio_p = log(provratio_p)
gen logprovratio_p_D = D.logprovratio_p

gen one = 1

label variable log_gsp_a   "Log GSP"
label variable log_gsp_a_D   "FD Log GSP"
label variable lpop   "Log Population"
label variable lpop2   "Log Population2"
label variable loginc   "Log Income"
label variable loginc2   "Log Income2"
label variable logexp   "Log Expenditure"
label variable logexp2   "Log Expenditure2"
label variable loglegexp   "Log Leg Expenditure"
label variable loglegexp2   "Log Leg Expenditure2"
label variable dem_control   "Democratic Control"
label variable logrev   "Log Revenue"
label variable logrev2   "Log Revenue2"

* Adding Var Labels for Table A1 

label variable logprovratio_D "FD Log Provisions / Words"
label variable logprovratio_p_D "FD Log Provisions / Pages"
label variable lpop_D "FD Log Population"
label variable lestab_D "FD Log Number Estabilishments"
label variable lprofits_mill_D "FD Log State Profits, Millions"
label variable real1incpc "FD Log Real Income, Thousands"
label variable lcbp_emp_D "FD Log Employment"
label variable state_news_uncertain "Econ State News Ratio"

label variable F_provisions_18_D "Lead FD Log Provisions"
label variable L_provisions_18_D "Lag FD Log Provisions"
label variable L2_provisions_18_D "2Lag FD Log Provisions"
label variable log_word_count_18 "Log Words"
gen log_page=log(page_count)
label variable log_page "Log Pages"
gen sent_prov_ratio=provisions_18/sentences_count
label variable sent_prov_ratio "Provisions / Sentences"
label variable pc1_compl18 "PCA"
label variable shock_prov18 "Instrument Provisions"
gen log_pages_D = D.log_page
label variable log_pages_D "FD Log Pages"

gen profit_ratio= profits_mill/estab
gen large_ratio= estab_large/estab
gen large_small_ratio= estab_large/estab_small

foreach x in profit_ratio large_ratio large_small_ratio  {
gen l`x' = log(`x')
gen l`x'_D = D.l`x'

}

foreach x in profit_ratio large_ratio large_small_ratio  {
gen `x'_D = D.`x'

}

gen period6070=0
replace period6070=1 if year <1980 & year>1958
gen period8090=0
replace period8090=1  if year <2000 & year>1978
gen period0010=0
replace period0010=1  if year>1998

gen decade60=0
replace decade60=1 if year<1970 & year>1958
gen decade70=0
replace decade70=1 if year<1980 & year>1968
gen decade80=0
replace decade80=1 if year<1990 & year>1978
gen decade90=0
replace decade90=1 if year<2000 & year>1988
gen decade00=0
replace decade00=1 if year<2010 & year>1998

foreach x in 6 12 18 24 30 36 42 48 {

foreach y in cont_prov cond_prov cond_list_prov {

gen logX_diff`y'_`x' = log_`y'_`x' - log_non_`y'_`x'

}
}
foreach x in econ soc proc fisc {

gen logX_diff_cond_prov_list42_`x'= log_cond_list_42`x' - log_non_cond_list_42`x'


}
keep if year>1964


merge 1:1 state year using statutes_state_bien
drop _merge

merge 1:1 state_abbrev year using data_amend_repeal
drop _merge

merge 1:1 state_abbrev year using data_amend_repeal_error
drop _merge

foreach x in  amend_clause_count repeal_clause_count  {

gen share_`x' = `x'/sentences_count
gen log_share_`x' = log(share_`x')

}

drop if state==.

save data_ready.dta, replace